﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DataCoreTechnologies.Scribe
{
   public enum AllowedValueType
   {
      Number,
      Boolean,
      Any
   }
   public partial class EditPropertyValueForm : Form
   {
      public EditPropertyValueForm(string description, string value, AllowedValueType type)
      {
         InitializeComponent();
         _description = description;
         _initialValue = value;
         _valueType = type;

         txtDescription.Text = _description;
         UpdateScreen();
      }

      public string Description
      {
         get
         {
            return _description;
         }
      }
      public string Value
      {
         get 
         {
            return _newValue;
         }
      }

      #region Form Events
      private void btnCancel_Click(object sender, EventArgs e)
      {
         _newValue = "";
         this.DialogResult = DialogResult.Cancel;
         this.Close();
      }
      private void btnOK_Click(object sender, EventArgs e)
      {
         switch (_valueType)
         {
            case AllowedValueType.Number:
               _newValue = txtNumericTextBox.Text;
               break;
            case AllowedValueType.Boolean:
               _newValue = cboBoolean.SelectedItem.ToString();
               break;
            case AllowedValueType.Any:
               _newValue = txtStringValue.Text; 
               break;
         }
         this.DialogResult = DialogResult.OK;
         this.Close();
      }
      private void btnReset_Click(object sender, EventArgs e)
      {
         txtStringValue.Text = _initialValue;
         UpdateScreen();
      }
      private void txtDescription_Enter(object sender, EventArgs e)
      {
         btnOK.Focus();
      }
      private void txtNumericTextBox_KeyPress(object sender, KeyPressEventArgs e)
      {
         if (_nonNumberEntered) e.Handled = true;
      }
      private void txtNumericTextBox_KeyDown(object sender, KeyEventArgs e)
      {
         _nonNumberEntered = false;
         if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
         {
            if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
            {
               if (e.KeyCode != Keys.Back) _nonNumberEntered = true;
            }
         }
         if (Control.ModifierKeys == Keys.Shift) _nonNumberEntered = true;
      }
      #endregion

      private void UpdateScreen()
      {
         switch (_valueType)
         {
            case AllowedValueType.Any:
               txtStringValue.Visible = true;
               txtNumericTextBox.Visible = false;
               cboBoolean.Visible = false;
               this.ActiveControl = txtStringValue;
               txtStringValue.Text = _initialValue;
               break;
            case AllowedValueType.Number:
               txtStringValue.Visible = false;
               txtNumericTextBox.Visible = true;
               cboBoolean.Visible = false;
               this.ActiveControl = txtNumericTextBox;
               txtNumericTextBox.Text = _initialValue;
               break;
            case AllowedValueType.Boolean:
               txtStringValue.Visible = false;
               txtNumericTextBox.Visible = false;
               cboBoolean.Visible = true;
               this.ActiveControl = cboBoolean;
               if (Convert.ToBoolean(_initialValue))
                  cboBoolean.SelectedIndex = 0;
               else
                  cboBoolean.SelectedIndex = 1;
               break;
         }
      }

      private string _description;
      private string _initialValue;
      private string _newValue;
      private AllowedValueType _valueType;
      private Boolean _nonNumberEntered = false;
   }
}
